//
//  ViewController.m
//  BiuWeather
//
//  Created by 楼楚 on 16/3/6.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//


#define IS_IOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
//主界面
#import "weatherView.h"
#import "ForecastControllerViewController.h"

#import "MBProgressHUD.h"
#import "LocationManager.h"

//获取网络数据
#import "GetWeatherData.h"
#import "WeatherData.h"

#import "ViewController.h"
//#import "CCLocationManager.h"

@interface ViewController ()<CLLocationManagerDelegate,UITableViewDelegate,weatherViewDelegate,GetWeatherDataDelegate,UIViewControllerTransitioningDelegate,MBProgressHUDDelegate,LocationManagerDelegate>{
    CLGeocoder         *_geocoder;
    MBProgressHUD      *_updatingHUD;
    int a;
    }
@property (nonatomic,strong)  LocationManager      *locationManager;
@property (nonatomic,strong)  GetWeatherData       *getWeatherData;
@property (nonatomic,strong)  UILabel              *textLabel;
@property (nonatomic,strong)  weatherView          *weatherView;
@property (nonatomic,strong)  NSString             *cityName;
@property (nonatomic,strong)  NSString             *cityCNName;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.locationManager = [[LocationManager alloc]init];
    self.locationManager.delegate = self;
    [self.locationManager start];
    a = 0 ;
    
    _updatingHUD = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:_updatingHUD];
    [self.view bringSubviewToFront:_updatingHUD];
    _updatingHUD.delegate = self;
    _updatingHUD.labelText = @"加载中...";
    [_updatingHUD show:YES];
    self.weatherView = [[weatherView alloc]initWithFrame:self.view.bounds];
    self.weatherView.layer.masksToBounds = YES;
    self.weatherView.delegate = self;
    [self.weatherView buildView];
    [self.view addSubview:self.weatherView];
    
    
    self.getWeatherData = [GetWeatherData new];
    self.getWeatherData.delegate = self;
//    [self.getWeatherData getData];
    
//    self.cityCNName = @"杭州市";
     }
- (void)mapManager:(LocationManager *)manager didUpdateAndGetLastCLLocation:(CLLocation *)location{
    NSLog(@"定位成功");
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    _geocoder = [[CLGeocoder alloc]init];
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark=[placemarks lastObject];
        
        NSString *str = [placemark.addressDictionary objectForKey:@"SubLocality"];
        self.cityCNName = str ;
        
        NSString *city1 = [str substringToIndex:([str  length]-1)];
        
        NSString *city2= [NSString stringWithFormat:@"city=%@",city1];
        self.cityName = [city2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@",self.cityName);
        [self performSelector:@selector(delayRunEvent:)
                   withObject:self.cityName
                   afterDelay:0.3f];
    }];

}

- (void)delayRunEvent:(id)object {
    if (a == 0) {
        NSLog(@"3-----3");
    NSLog(@"%@",object);
    self.getWeatherData.cityStr = object;
    
    [self.getWeatherData getData];

    }
    else
    {
        NSLog(@"zhix");
    }
    a = a + 1;
}
- (void)pullUpEvent{
    ForecastControllerViewController *forecastCV = [[ForecastControllerViewController alloc]initWithArray:self.getWeatherData.weatherData.week.oneWeek];
    forecastCV.transitioningDelegate = self;
    [self presentViewController:forecastCV
                       animated:YES
                     completion:^{}];
    
}
- (void)sucess:(BOOL)sucess{
    if (sucess) {

        WeatherData *dic0 = [[WeatherData alloc]initWithDictionary:self.getWeatherData.weather];
        self.weatherView.today = dic0.today;
        self.weatherView.weekForcast = dic0.week;
        self.weatherView.cityName = self.cityCNName;
        [_updatingHUD hide:YES];
        [self.weatherView show];
        

    }
}
- (void)pullDownToRefreshData
{
    [self.weatherView hide];
    [_updatingHUD show:YES];
    [self.locationManager start];
    a = 0;
   
}


//状态栏
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}
//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
